package com.bhex.kline.widget.tab;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.CornerPathEffect;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.AppCompatTextView;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.util.TypedValue;

import com.bhex.kline.R;
import com.bhex.kline.widget.util.PixelUtils;

/**
 * Created by BHEX.
 * User: gdy
 * Date: 2020/3/24
 * Time: 21:10
 */
public class TabTextView extends AppCompatTextView {

    public int type;

    private Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    private Paint mArrowPaint = new Paint(Paint.ANTI_ALIAS_FLAG);

    private int mSelectColor = Color.parseColor("#3375E0");

    private int mNormalColor = Color.parseColor("#919598");

    private Context mContext;
    private CornerPathEffect cornerPathEffect = new CornerPathEffect(PixelUtils.dp2px(getContext(),2));

    public TabTextView(Context context) {
        this(context,null);
    }

    public TabTextView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public TabTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context,attrs);
    }

    /**
     *
     * @param context
     * @param attrs
     */
    private void init(Context context, AttributeSet attrs) {
        mContext = context;
        mPaint.setDither(true);
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setColor(mSelectColor);
        mPaint.setStrokeWidth(PixelUtils.dp2px(mContext,1.5f));

        mArrowPaint.setDither(true);
        mArrowPaint.setStyle(Paint.Style.STROKE);
        mArrowPaint.setColor(mSelectColor);
        mArrowPaint.setStrokeCap(Paint.Cap.ROUND);
        mArrowPaint.setDither(true);
        mArrowPaint.setPathEffect(cornerPathEffect);
        mArrowPaint.setStrokeWidth(PixelUtils.dp2px(mContext,1f));

    }
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        setTextSize(TypedValue.COMPLEX_UNIT_SP, 12);
        Rect bound = new Rect();
        TextPaint textPaint = this.getPaint();
        textPaint.getTextBounds(getText().toString(),0, this.getText().toString().length(),bound);
        int center = getMeasuredHeight() / 2;
        int baseLine = (int) (center + (textPaint.getFontMetrics().bottom - textPaint.getFontMetrics().top)/2 - textPaint.getFontMetrics().bottom );
        int bottom = (int) (baseLine + textPaint.getFontMetrics().bottom);
        Drawable drawableRight = getCompoundDrawables()[2];
        int padding = 0;
        if(drawableRight!=null){
            padding = PixelUtils.dp2px(mContext,11);
        }
        int start = (getMeasuredWidth() - bound.width()-padding) / 2 ;
        int end = start + bound.width();

        if(isSelected()){
            mPaint.setColor(mSelectColor);
            mArrowPaint.setColor(mSelectColor);
            setTextColor(mSelectColor);
        }else{
            mArrowPaint.setColor(mNormalColor);
            mPaint.setColor(Color.TRANSPARENT);
            setTextColor(mNormalColor);
        }
        //绘制三角箭头
        if(type==2){
            Path path = new Path();
            int endx = end-PixelUtils.dp2px(mContext,0.5f);
            int endy = baseLine+PixelUtils.dp2px(mContext,3);
            path.moveTo(endx,endy);
            path.lineTo(endx+PixelUtils.dp2px(mContext,6f),endy);
            path.lineTo(endx+PixelUtils.dp2px(mContext,6f),endy-PixelUtils.dp2px(mContext,6));
            path.close();
            mArrowPaint.setStyle(Paint.Style.FILL);
            canvas.drawPath(path,mArrowPaint);
        }



        if(type==2 && !isSelected()){

        }else{
            float height = bottom+PixelUtils.dp2px(mContext,5);
            canvas.drawLine(start, height, end, height, mPaint);
        }



        //canvas.drawCircle(end+PixelUtils.dp2px(mContext,6),center,PixelUtils.dp2px(mContext,6),mPaint);
    }

    public int getSelectColor() {
        return mSelectColor;
    }

    public void setSelectColor(int mSelectColor) {
        this.mSelectColor = mSelectColor;
    }

    public int getNormalColor() {
        return mNormalColor;
    }

    public void setNormalColor(int mNormalColor) {
        this.mNormalColor = mNormalColor;
    }


}
